package com.lineage.server.serverpackets;

import com.lineage.DatabaseFactory;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class S_PledgeWarehouseHistory extends ServerBasePacket {
  private static final String S_PledgeWarehouseHistory = "[S] S_PledgeWarehouseHistory";
  
  private byte[] _byte = null;
  
  public S_PledgeWarehouseHistory(int clanId) {
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("DELETE FROM clan_warehouse_history WHERE clan_id=? AND record_time < ?");
      pstm.setInt(1, clanId);
      pstm.setTimestamp(2, new Timestamp(System.currentTimeMillis() - 259200000L));
      pstm.execute();
      pstm.close();
      pstm = con.prepareStatement("SELECT * FROM clan_warehouse_history WHERE clan_id=? ORDER BY id DESC");
      pstm.setInt(1, clanId);
      rs = pstm.executeQuery();
      rs.last();
      int rowcount = rs.getRow();
      rs.beforeFirst();
      writeC(250);
      writeC(117);
      writeD(rowcount);
      while (rs.next()) {
        writeS(rs.getString("char_name"));
        writeC(rs.getInt("type"));
        writeS(rs.getString("item_name"));
        writeD(rs.getInt("item_count"));
        writeD((int)((System.currentTimeMillis() - rs.getTimestamp("record_time").getTime()) / 60000L));
      } 
    } catch (SQLException e) {
      System.out.println(e.getLocalizedMessage());
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = this._bao.toByteArray(); 
    return this._byte;
  }
  
  public String getType() {
    return "[S] S_PledgeWarehouseHistory";
  }
}
